#include <cstdio>

#define comb(a, b) ((a)*10+(b))
#define sq(a) ((a)*(a))

int S1[] = {2, 3, 5, 8};
int S2[] = {1, 4, 6, 7}; 

int A[8];
bool book[9];
int ans;

void check() {
	int a = sq(comb(A[0], A[1])) + sq(comb(A[2], A[3])) + 
			sq(comb(A[4], A[5])) + sq(comb(A[6], A[7]));
	int b = sq(comb(A[1], A[0])) + sq(comb(A[3], A[2])) + 
			sq(comb(A[5], A[4])) + sq(comb(A[7], A[6]));
	if (a==b) {
		for (int i=0; i<8; i++) {
			printf("%d ", A[i]);
		}
		printf("\n");
		ans++;
	}
}

void dfs(int n) {
	if (n >= 8) {
		check();
		return;
	}
	if (n%2==0) {
		for (int i=0; i<4; i++) {
			int d = S1[i];
			if (book[d]) continue;
			book[d] = true;
			A[n] = d;
			dfs(n+1);
			book[d] = false;
		}
	} else {
		for (int i=0; i<4; i++) {
			int d = S2[i];
			if (book[d]) continue;
			book[d] = true;
			A[n] = d;
			dfs(n+1);
			book[d] = false;
		}
	}
//	for (int i=1; i<=8; i++) {
//		if (book[i]) continue;
//		book[i] = true;
//		A[n] = i;
//		dfs(n+1);
//		book[i] = false;
//	}
}

int main() {
	dfs(0);
	printf("\n\nans==%d\n", ans);
	return 0;
}
